<?php 
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
include 'config.php';
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Content-Length: ' . strlen($jsonStr)
    )
        );
    $response = curl_exec($ch);
    
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    return ['code'=>$httpCode, 'data'=>$response];
}
function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    
    $res = curl_exec($curl);
    curl_close($curl);
    
    return $res;
}
function getToken($appId,$secret){
    $file='/var/www/elixir07/web/cache/access_token.json';
    $data = json_decode(file_get_contents($file));
    if ($data->expire_time < time()) {
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appId.'&secret='.$secret;
        $res = json_decode(httpGet($url));
        $access_token = $res->access_token;
        if ($access_token) {
            $data->expire_time = time() + 4000;
            $data->access_token = $access_token;
            $fp = fopen($file, "w");
            fwrite($fp, json_encode($data));
            fclose($fp);
        }
    } else {
        $access_token = $data->access_token;
    }
    return $access_token;
}
$access_token=getToken($appId,$secret);
$url='https://api.weixin.qq.com/cgi-bin/express/business/path/get?access_token='.$access_token;
$pathItemList=array();
if (isset($_POST['op'])){
    $openid=$_POST['op'];
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {}
    $query=$mysqli->query("SELECT waybill_id,createtime FROM `orders` where openid='".$openid."' limit 0,1 ");
    if($query->num_rows>0){
        $getOrder=$query->fetch_array();
        $waybill_id=$getOrder['waybill_id']; //3103674248663
        $sendTime=strtotime($getOrder['createtime']) + 60*60*24*3;
        if (empty($waybill_id)){
            echo json_encode(array('id'=>'no','p'=>$pathItemList,'t'=>''));
            exit;
        }
    }else{
        echo json_encode(array('id'=>'no','p'=>$pathItemList,'t'=>''));
        exit;
    }
    
}

$openid='ohFb-40Zv6S5tbQpvCHzpjIfUrKo';
$order_id='0123456789011112';
 
$orderInfo=array(
    'order_id'=> $order_id,
    'openid'=>$openid,
    'delivery_id'=> 'YUNDA',
    "waybill_id"=> $waybill_id
);
 
$jsonInfo=json_encode($orderInfo,  JSON_UNESCAPED_UNICODE);
  
$getdata=http_post_json($url, $jsonInfo);
/**
$getdata['data']='{
  "openid": "OPENID",
  "delivery_id": "SF",
  "waybill_id": "123456789012",
  "path_item_num": 3,
  "path_item_list": [
    {
      "action_time": 1533052800,
      "action_type": 100001,
      "action_msg": "快递员已成功取件"
    },
    {
      "action_time": 1533062800,
      "action_type": 200001,
      "action_msg": "快件已到达xxx集散中心，准备发往xxx"
    },
    {
      "action_time": 1533072800,
      "action_type": 300001,
      "action_msg": "快递员已出发，联系电话xxxxxx"
    }
  ]
}';
*/
$pathInfo=json_decode($getdata['data'], true);


foreach ($pathInfo['path_item_list'] as $v){
    $tmptime=explode(' ', date("Y-m-d H:i:s",$v['action_time']));
    $getTime=explode('-', $tmptime[0]);
    
    $pathItemList[]=array('t'=>$getTime[0].'年'.$getTime[1].'月'.$getTime[2].'日','m'=>$v['action_msg']);
}
$tmpDay=explode('-', date("Y-m-d",$sendTime));
echo json_encode(array('id'=>$pathInfo['waybill_id'],'p'=>$pathItemList,'t'=>$tmpDay[0].'年'.$tmpDay[1].'月'.$tmpDay[2].'日' ));

//var_dump($pathInfo);

?>
 